//Copyright 2010 Balazs Keri, Imre Keri
//
//This file is part of FireworksCreator.
//
//FireworksCreator is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//FireworksCreator is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with FireworksCreator.  If not, see <http://www.gnu.org/licenses/>.
//
#include "multitypedatacontainer.h"
#include "multitypedata.h"
#include <common/restoreerror.h>
//
namespace model
{
MultiTypeDataContainer::MultiTypeDataContainer()
{
}
MultiTypeDataContainer::MultiTypeDataContainer(MultiTypeData *p_data)
:
   data(p_data)
{
}
MultiTypeDataContainer::~MultiTypeDataContainer()
{
}
//
void MultiTypeDataContainer::store(QDataStream &p_stream)const
{
   if(data)
   {
      p_stream<<data->get_type();
      data->store(p_stream);
   }
   else
   {
      p_stream<<type_null;
   }
}
void MultiTypeDataContainer::restore(QDataStream &p_stream)
{
   data.reset();
   int type;
   p_stream>>type;
   if(type!=type_null)
   {
      MultiTypeData *d=create_data(type);
      if(!d) throw(RestoreError("unknown type at MultiTypeDataContainer::restore"));
      d->restore(p_stream);
      this->data=d;
   }
}
//
const MultiTypeData *MultiTypeDataContainer::get_data()const
{
   Q_ASSERT(data);
   return data.data();
}
MultiTypeData *MultiTypeDataContainer::get_data()
{
   Q_ASSERT(data);
   data.detach();
   return data.data();
}
int MultiTypeDataContainer::get_type()const
{
   if(data) return data->get_type();
   else return type_null;
}
}
//
QDataStream &operator<<(QDataStream &p_stream,const model::MultiTypeDataContainer &p_data)
{
   p_data.store(p_stream);
   return p_stream;
}
QDataStream &operator>>(QDataStream &p_stream,model::MultiTypeDataContainer &p_data)
{
   p_data.restore(p_stream);
   return p_stream;
}
